import { routerAll } from '@/router'

const state = {
  CURRENT_NAV: '/',
  CURRENT_SIDE_NAV: '/',
  ROUTES: routerAll,
  ROUTES_CHILDREN: [],
  CACHE_ROUTES: [{
    path: 'index',
    meta: {
      title: '首页'
    },
    name: 'index'
  }]
}

const mutations = {
  // routes_children
  SET_ROUTES_CHILDREN(state, data) {
    state.ROUTES_CHILDREN = data
  },
  // current_nav
  SET_CURRENT_NAV(state, data) {
    state.CURRENT_NAV = data
  },
  // current
  SET_CURRENT_SIDE_NAV(state, data) {
    state.CURRENT_SIDE_NAV = data
  },
  // cache_routes
  SET_CACHE_ROUTES(state, data) {
    if (state.CACHE_ROUTES.filter(f => f.name === data.name).length === 0) {
      state.CACHE_ROUTES.push(data)
      state.CURRENT_NAV = data.name
    }
  },
  // delete_cache_routes
  DELETE_CACHE_ROUTES(state, name) {
    state.CACHE_ROUTES = state.CACHE_ROUTES.filter(f => f.name !== name)
    state.CURRENT_SIDE_NAV = state.CACHE_ROUTES[state.CACHE_ROUTES.length - 1].name || '/'
  }
}

const actions = {
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
